﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RestService.Processing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace RestService.Processing
{
    public class PointClustered:DoiTuong
    {
        #region double X property
        public double X
        {
            get
            {
                if (_DataRow["X"] is double) return (double)_DataRow["X"];
                return 0;
            }
            set
            {
                _DataRow["X"] = (double)value;
            }
        }
        #endregion

        #region double Y property
        public double Y
        {
            get
            {
                if (_DataRow["Y"] is double) return (double)_DataRow["Y"];
                return 0;
            }
            set
            {
                _DataRow["Y"] = (double)value;
            }
        }
        #endregion

        #region double AVGRSS_TOP3MAX property
        public double AVGRSS_TOP3MAX
        {
            get
            {
                if (_DataRow["AVGRSS_TOP3MAX"] is double) return (double)_DataRow["AVGRSS_TOP3MAX"];
                return 0;
            }
            set
            {
                _DataRow["AVGRSS_TOP3MAX"] = (double)value;
            }
        }
        #endregion

        #region double AVGRSS_ALL property
        public double AVGRSS_ALL
        {
            get
            {
                if (_DataRow["AVGRSS_ALL"] is double) return (double)_DataRow["AVGRSS_ALL"];
                return 0;
            }
            set
            {
                _DataRow["AVGRSS_ALL"] = (double)value;
            }
        }
        #endregion

        #region double ClusterID_TOP3MAX property
        public double ClusterID_TOP3MAX
        {
            get
            {
                if (_DataRow["ClusterID_TOP3MAX"] is double) return (double)_DataRow["ClusterID_TOP3MAX"];
                return 0;
            }
            set
            {
                _DataRow["ClusterID_TOP3MAX"] = (double)value;
            }
        }
        #endregion

        #region double ClusterID_ALL property
        public double ClusterID_ALL
        {
            get
            {
                if (_DataRow["ClusterID_ALL"] is double) return (double)_DataRow["ClusterID_ALL"];
                return 0;
            }
            set
            {
                _DataRow["ClusterID_ALL"] = (double)value;
            }
        }
        #endregion

        public PointClustered(DataRow dataRow)
            : base(dataRow)
        {
            DanhMucPointClustered dm = new DanhMucPointClustered();
            if (this.ID <= 0) this.ID = dm.GetNextID();
        }
        public int GetMaxID()
        {
            DanhMucPointClustered dm = new DanhMucPointClustered();
            return dm.GetNextID();
        }
    }

    public class DanhMucPointClustered : DanhMucDoiTuong, IEnumerable {
        #region Instance property: Singleton Design pattern
        private static DanhMucPointClustered _Instance;
        public static DanhMucPointClustered Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucPointClustered();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                else _Instance.LoadFromDB();
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "PointClustered";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;
            private DanhMucPointClustered danhMucPointClustered;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            public TokenEnumerator(DanhMucPointClustered danhMucPointClustered)
            {
                // TODO: Complete member initialization
                this.danhMucPointClustered = danhMucPointClustered;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new AP((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public PointClustered this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new PointClustered(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public PointClustered NewDoiTuong()
        {
            return new PointClustered(DataTable.NewRow());
        }

        public PointClustered GetByProperty(string property, object value)
        {
            DataRow[] rows = GetDataRows(property, value);
            if (rows.Length > 0) return new PointClustered(rows[0]);
            else return null;
        }

        public PointClustered GetByID(int objID)
        {
            return GetByProperty("PID", objID);
        }

        public static DanhMucPointClustered GetAll()
        {
            return Instance;
        }
        
        public static DanhMucPointClustered Filter(string filterExpression)
        {
            try
            {
                DanhMucPointClustered obj = new DanhMucPointClustered();
                obj.DataView = new DataView(DanhMucPointClustered.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DataTable GetListClusterAndAVGRSS()
        {
            SqlConnection con = new SqlConnection(Common.ConnectionString);
            try
            {
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = new SqlDataAdapter();

                cmd.CommandText = "SELECT ClusterID_TOP3MAX, AVG(AVGRSS_TOP3MAX) AVGRSS_TOP3MAX \n" +
                                    "FROM PointClustered \n" +
                                    "GROUP BY ClusterID_TOP3MAX \n";
                
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
        public static DataTable GetListClusterByClusterID(string clusterID)
        {
            SqlConnection con = new SqlConnection(Common.ConnectionString);
            try
            {
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = new SqlDataAdapter();

                cmd.CommandText = string.Format("SELECT * FROM PointClustered WHERE ClusterID_TOP3MAX = {0}", clusterID);

                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}